request = {"a": 1, "b": 2, "c": 3}


class Worker:
    def __init__(self, nexter=None):
        self.nexter = nexter

    def set_container(self, container):
        """设置责任链的容器"""
        self.container = container

    def transmit(self, request):
        """向下传递"""
        temp = self.handle_request(request)  # 当前处理环节 处理任务
        self.container.update(temp)  # 容器整合当前结果
        if self.nexter:
            self.nexter.set_container(self.container)  # 容器向下传递
            self.nexter.transmit(request)  # 任务向下传递
        else:
            print("任务结束")


class R1(Worker):
    def handle_request(self, request):
        print("r1")
        return {"r1": request.get("a") + 10}


class R2(Worker):
    def handle_request(self, equest):
        print("r2")
        return {"r2": request.get("b") * 8}


class R3(Worker):
    def handle_request(self, request):
        print("r3")
        return {"r3": request.get("c") * 10}


work = R1(R2(R3()))
work.set_container({})
work.transmit(request)
print(work.container)

work = R1(R2())
work.set_container({})
work.transmit(request)
print(work.container)

work = R1(R3())
work.set_container({})
work.transmit(request)
print(work.container)

work = R2(R3())
work.set_container({})
work.transmit(request)
print(work.container)

work = R3(R1())
work.set_container({})
work.transmit(request)
print(work.container)
